package cn.iocoder.yudao.module.of.controller.admin.meetingminutes;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.meetingminutes.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingminutes.MeetingMinutesDO;
import cn.iocoder.yudao.module.of.service.meetingminutes.MeetingMinutesService;

@Tag(name = "管理后台 - 会议纪要")
@RestController
@RequestMapping("/of/meeting-minutes")
@Validated
public class MeetingMinutesController {

    @Resource
    private MeetingMinutesService meetingMinutesService;

    @PostMapping("/create")
    @Operation(summary = "创建会议纪要")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:create')")
    public CommonResult<Long> createMeetingMinutes(@Valid @RequestBody MeetingMinutesSaveReqVO createReqVO) {
        return success(meetingMinutesService.createMeetingMinutes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会议纪要")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:update')")
    public CommonResult<Boolean> updateMeetingMinutes(@Valid @RequestBody MeetingMinutesSaveReqVO updateReqVO) {
        meetingMinutesService.updateMeetingMinutes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会议纪要")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:delete')")
    public CommonResult<Boolean> deleteMeetingMinutes(@RequestParam("id") Long id) {
        meetingMinutesService.deleteMeetingMinutes(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会议纪要")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:query')")
    public CommonResult<MeetingMinutesRespVO> getMeetingMinutes(@RequestParam("id") Long id) {
        MeetingMinutesDO meetingMinutes = meetingMinutesService.getMeetingMinutes(id);
        return success(BeanUtils.toBean(meetingMinutes, MeetingMinutesRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得会议纪要")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:query')")
    public CommonResult<List<MeetingMinutesRespVO>> getMeetingMinutes(  MeetingMinutesPageReqVO pageReqVO) {
        List<MeetingMinutesRespVO> meetingMinutes = meetingMinutesService.getMeetingMinutesList(pageReqVO);
        return success(BeanUtils.toBean(meetingMinutes, MeetingMinutesRespVO.class));
    }

    @GetMapping("/getByMeetingId")
    @Operation(summary = "根据会议ID获得会议纪要列表")
    @Parameter(name = "meetingId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:query')")
    public CommonResult<List<MeetingMinutesRespVO>> getMinuteList(  @RequestParam Long meetingId) {
        List<MeetingMinutesRespVO> meetingMinutes = meetingMinutesService.getMinuteList(meetingId);
        return success(BeanUtils.toBean(meetingMinutes, MeetingMinutesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会议纪要分页")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:query')")
    public CommonResult<PageResult<MeetingMinutesRespVO>> getMeetingMinutesPage(@Valid MeetingMinutesPageReqVO pageReqVO) {
        PageResult<MeetingMinutesDO> pageResult = meetingMinutesService.getMeetingMinutesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MeetingMinutesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会议纪要 Excel")
    @PreAuthorize("@ss.hasPermission('of:meeting-minutes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMeetingMinutesExcel(@Valid MeetingMinutesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MeetingMinutesDO> list = meetingMinutesService.getMeetingMinutesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会议纪要.xls", "数据", MeetingMinutesRespVO.class,
                        BeanUtils.toBean(list, MeetingMinutesRespVO.class));
    }

}